/*
 * Copyright 2007-2012, Sualeh Fatehi <sualeh@hotmail.com>
 * 
 * This work is licensed under the Creative Commons Attribution 3.0 License. 
 * To view a copy of this license, visit 
 * http://creativecommons.org/licenses/by/3.0/ 
 * or send a letter to 
 * Creative Commons
 * 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 */
package sf.event.controller;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sf.event.model.Event;
import sf.event.service.EventService;

@Controller
public class EventController
{

  @Autowired
  private EventService eventService;

  @RequestMapping(value = "/event_add", method = RequestMethod.POST)
  public ModelAndView addEvent(final HttpServletRequest request)
  {
    final String name = request.getParameter("name");
    if (name != null)
    {
      final Event event = new Event();
      event.setName(name);
      event.setStartDate(new Date());
      event.setDuration(10);

      eventService.save(event);
    }

    return new ModelAndView("event_view", "eventList", eventService.findAll());
  }

  @RequestMapping("/event_delete/{eventId}")
  public String deleteEvent(@PathVariable("eventId") final Long eventId)
  {
    if (eventId != null)
    {
      eventService.delete(eventId);
    }

    return "event_view";
  }

  @RequestMapping("/event_view")
  public String listEvents(final Map<String, Object> map)
  {
    map.put("eventList", eventService.findAll());

    return "event_view";
  }

}
